package com.ygqh.baby.service.impl;
import java.util.Date;
import java.util.List;

import com.ygqh.baby.ao.InvoiceStatus;
import  com.ygqh.baby.ao.QueryInfo;
import  com.ygqh.baby.ao.ResultSet;
import  com.ygqh.baby.po.AdminInvoice;
import com.ygqh.baby.po.AdminInvoiceDetail;
import com.ygqh.baby.po.AdminInvoiceDetailExample;
import com.ygqh.baby.mapper.AdminInvoiceDetailMapper;
import  com.ygqh.baby.mapper.AdminInvoiceMapper;
import  com.ygqh.baby.service.AdminInvoiceService;

import  org.springframework.util.Assert;

import java.util.Arrays;

import  org.springframework.stereotype.Service;
import  org.springframework.beans.factory.annotation.Autowired;

import  com.ygqh.baby.po.AdminInvoiceExample;
import  com.ygqh.baby.ao.DataStatus;

@Service
public class AdminInvoiceServiceImpl implements AdminInvoiceService{
	
	@Autowired
	private AdminInvoiceMapper adminInvoiceMapper;
	@Autowired
	private AdminInvoiceDetailMapper adminInvoiceDetailMapper;
	
	
	@Override
	public List<AdminInvoice> find() {
		AdminInvoiceExample example = new AdminInvoiceExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return adminInvoiceMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		AdminInvoiceExample example = new AdminInvoiceExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return adminInvoiceMapper.countByExample(example);
	}

	@Override
	public AdminInvoice findById(Long id) {
		AdminInvoice invoie= adminInvoiceMapper.selectByPrimaryKey(id);
		AdminInvoiceDetailExample example=new AdminInvoiceDetailExample();
		example.createCriteria().andInvoiceIdEqualTo(id).andStatusEqualTo(DataStatus.Valid.name());
		invoie.setAdminInvoiceDetails(adminInvoiceDetailMapper.selectByExample(example));
		return invoie;
	}


	

	@Override
	public int save(AdminInvoice adminInvoice) {
		adminInvoiceMapper.insertSelective(adminInvoice);
		
		adminInvoiceDetailMapper.saveBatch(adminInvoice.getId(),adminInvoice.getAdminInvoiceDetails());
		return 1;
	}

	@Override
	public int update(AdminInvoice adminInvoice) {
		 adminInvoiceMapper.updateByPrimaryKeySelective(adminInvoice);
		 //先删除发票下的明细，再添加
		 AdminInvoiceDetailExample example=new AdminInvoiceDetailExample();
		 example.createCriteria().andInvoiceIdEqualTo(adminInvoice.getId());
		 adminInvoiceDetailMapper.deleteByExample(example);	
		 if(!(adminInvoice.getInvoiceStatus()!=null&&adminInvoice.getInvoiceStatus().equals(InvoiceStatus.Cancel)))
		 adminInvoiceDetailMapper.saveBatch(adminInvoice.getId(),adminInvoice.getAdminInvoiceDetails());
		 return 1;
	}

	@Override
	public ResultSet<AdminInvoice> search(QueryInfo queryInfo,String q,String applyPerson,Date startDate,Date endDate, String invoiceTitle) {
		List<AdminInvoice>  list =  adminInvoiceMapper.selectSuper(queryInfo, q, applyPerson, startDate, endDate, invoiceTitle);
		int count = adminInvoiceMapper.countSuper(q, applyPerson, startDate, endDate, invoiceTitle);
		ResultSet<AdminInvoice> resultSet = new ResultSet<AdminInvoice>(count,list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		AdminInvoiceExample example = new AdminInvoiceExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		AdminInvoice record = new AdminInvoice();
		record.setStatus(DataStatus.Delete);

		return adminInvoiceMapper.updateByExampleSelective(record, example);
	}
	
	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[]{id});
	}

	@Override
	public int saveOrUpdate(AdminInvoice adminInvoice) {
		int r = 0;
		if(adminInvoice.getId() != null) {
			r = this.update(adminInvoice);
		}else {
			r = this.save(adminInvoice);
		}
		return r;
	}

	@Override
	public Boolean isExistsInvoice(Long id, String invoiceNo) {		
		return adminInvoiceMapper.isExistInvoice(id,invoiceNo);
	}
	

}

